package common;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.text.SimpleDateFormat;
import java.time.Duration;

public class Utils {
    static SimpleDateFormat curTimeFormat = new SimpleDateFormat("YYYY-MM-dd_HH:mm:ss:SS");
    static SimpleDateFormat mailTimeFormat = new SimpleDateFormat("YYYY-MM-dd-HHmmssSS");
    public static String curTime = curTimeFormat.format(System.currentTimeMillis());
    public static String mailTime = mailTimeFormat.format(System.currentTimeMillis());
    public static WebDriver driver = null;
    //显示等待
    public WebDriverWait wait = null;


    //创建随机手机号
    public static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");

    public static int getNum(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }

    public static String getTel() {
        int index = getNum(0, telFirst.length - 1);
        String first = telFirst[index];
        String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
        String third = String.valueOf(getNum(1, 9100) + 10000).substring(1);
        return first + second + third;
    }


    public Utils(String URL) {
        driver = Utils.createDriver();
        driver.get(URL);
        //显示等待
        wait = new WebDriverWait(driver, Duration.ofSeconds(3));
    }

    public static WebDriver createDriver() {
        if (driver == null) {
            synchronized ((Utils.class)) {
                //创建驱动
                WebDriverManager.chromedriver().setup();
                //添加配置:允许访问所有链接
                ChromeOptions options = new ChromeOptions();
                options.addArguments("--remote-allow-origins=*");
                //创建驱动对象
                driver = new ChromeDriver(options);
                //隐式等待
                driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
            }
        }
        return driver;
    }

    public  void screenShot(String str) {
        try {
            //年月日
            SimpleDateFormat sim1 = new SimpleDateFormat("YYYY-MM-dd");
            //时分秒
            SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");
            String dirTime = sim1.format(System.currentTimeMillis());
            String imageTime = sim2.format(System.currentTimeMillis());
            //图片文件名:路径+图片名称
            String fileName = "./src/test/java/images/" + dirTime + "/" + str + "-" + imageTime + ".png";
            File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(file, new File(fileName));
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常信息
        }
    }

    public static void quit() {
        if (driver != null) {
            driver.quit();
        }
    }


}
